package iot

import (
	"errors"
	"gorm.io/gorm"
	"iot-base/common/dbobj"
)

var ProtocolTable = "t_model_protocols"

type (
	Protocol dbobj.TModelProtocol

	ProtocolMode struct {
		db *gorm.DB
	}
)

func NewProtocolMode(db *gorm.DB) *ProtocolMode {
	return &ProtocolMode{
		db,
	}
}
func (p *ProtocolMode) GetProtocolByModelIds(modelIds ...string) ([]Protocol, error) {
	var resp []Protocol
	err := p.db.Table(ProtocolTable).Where("model_id in ?", modelIds).Find(&resp).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return resp, nil
	}
	return append([]Protocol{}, resp...), nil
}
